//
//  CommunityViewController.m
//  jiyujiaProgram
//
//  Created by 中良 赵 on 15/10/13.
//  Copyright (c) 2015年 中良 赵. All rights reserved.
//

#import "CommunityViewController.h"
#import "AusleseCell.h"
#import "CardCell.h"

#import "CardDetailViewController.h"
#import "CreatCardViewController.h"

#import "CardCell.h"
#import "CircleModel.h"

#import "CardDetailViewController.h"
#import "CreatCardViewController.h"

@interface CommunityViewController ()<XLCycleScrollViewDatasource,XLCycleScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,AusleseCellDelegate>

@end

@implementation CommunityViewController
{
    TeacherModel *_teacherModel;
    MemberModel *_memberModel;
    
    NSMutableArray *_dataArray;
    UIView *_cardView;
    UICollectionView *_cardCollection;
    CircleModel *_circleModel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self creatNavigationBarWithImage:nil title:@"社区"];
    
    [self creatCardView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [self loadUserInfo];
    [self creatHttpRequest];
}


- (void)loadUserInfo
{
    UserInfoManager *userInfo = [UserInfoManager sharedUserInfoManager];
    [userInfo loadUserInfoFromSanbox];
    _memberModel = userInfo.memberModel;
    _teacherModel = userInfo.teacherModel;
}



- (void)creatHttpRequest
{
    NSDictionary *param = @{
                            @"pageIndex":@"0",
                            @"pageSize":@"999",
                            };
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(CIRCLE) params:param complete:^(BOOL successed, NSDictionary *result) {
        if (successed) {
            [CircleModel setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data":@"CircleData",
                         };
            }];
            
            if (_dataArray) {
                [_dataArray removeAllObjects];
            }else{
                _dataArray = [[NSMutableArray alloc] init];
            }
            
            _circleModel = [[CircleModel alloc] init];
            _circleModel = [CircleModel objectWithKeyValues:result];
            [_dataArray addObjectsFromArray:_circleModel.data];
            [_cardCollection reloadData];
        }
    }];
}


- (void)creatCardView
{
    _cardView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ECSCREEN_W, ECSCREEN_H - 64 - 49)];
    [self.view addSubview:_cardView];
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    CGFloat w = (ECSCREEN_W - 30)/2;
    layout.itemSize = CGSizeMake(w, w - 20);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset =  UIEdgeInsetsMake(10, 10, 10, 10);
    
    _cardCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_W, ECSCREEN_H - 64 - 49) collectionViewLayout:layout];
    _cardCollection.backgroundColor = BG_COLOR;
    
    _cardCollection.delegate = self;
    _cardCollection.dataSource = self;
    
    [_cardCollection registerNib:[UINib nibWithNibName:@"CardCell" bundle:nil] forCellWithReuseIdentifier:@"cardCell"];
    
    [_cardView addSubview:_cardCollection];
}



#pragma mark - numOfSections
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


#pragma mark - numOfItems
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}


#pragma mark - cellForItemIndexPath
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseIdentifier = @"cardCell";
    CardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.clipsToBounds = YES;
    cell.layer.cornerRadius = 3;
    cell.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [cell setCellWithModel:_dataArray WithIndexPath:indexPath];
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CircleData *data = _dataArray[indexPath.row];
    CardDetailViewController *vc = [[CardDetailViewController alloc] init];
    vc.circle_Id = data.circle_Id;
    vc.circle_name = data.name;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
